package com.snow.service.options.impl;

import com.snow.dao.OptionDao;
import com.snow.pojo.model.OptionsDomain;
import com.snow.service.options.OptionsService;
import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * @Description:
 * @author: snow
 * @Date: 2020-01-19
 **/
@Service
public class OptionsServiceImpl implements OptionsService {
    private static final Logger logger = Logger.getLogger(OptionsServiceImpl.class);
    @Autowired
    private OptionDao optionDao;

    /**
     * 删除网站配置
     * @param name
     */
    @Override
    @CacheEvict(value={"optionCache","optionsCache"},allEntries = true,beforeInvocation = true)//设置为方法执行前清除缓存
    public void deleteOptionByName(String name) {
        logger.debug("Enter deleteOptionsByName in OptionsServiceImpl");
        if(StringUtils.isBlank(name)){
            optionDao.deleteOptionByName(name);
            logger.debug("Exit deleteOptionByName in OptionServiceImpl");
        }else{
            throw new RuntimeException("Arg: name is null");
        }
    }

    /**
     * 更新网站设置
     * @param name
     * @param value
     */
    @Override
    @Transactional
    @CacheEvict(value={"optionCache","optionsCache"},allEntries = true,beforeInvocation = true)
    public void updateOptionByName(String name, String value) {
        logger.debug("Enter updateOpytionsByName in OptionServiceImpl");
        if(StringUtils.isBlank(name)){
            throw new RuntimeException("Args name is null needed not null");
        }
        OptionsDomain domain = new OptionsDomain();
        domain.setName(name);
        domain.setValue(value);
        optionDao.updateOptionByName(name,value);
    }

    /**
     * 批量保存网站设置
     * @param option
     */
    @Override
    @Transactional
    @CacheEvict(value={"optionsCache","optionCache"},allEntries = true,beforeInvocation = true)
    public void saveOption(Map<String, String> option) {
        if(option != null && !option.isEmpty()){
            option.forEach(this::updateOptionByName);
        }else{
            throw new RuntimeException("Args option is null or Empty;");
        }

    }

    /**
     * 获取某一设置
     * @param name
     * @return
     */
    @Override
    @Cacheable(value="optionCache",key="'optionByname_'+#p0")
    public OptionsDomain getOptionsByName(String name) {
        if(StringUtils.isBlank(name)){
            throw new RuntimeException("Arg name is null must be not null");
        }
        return optionDao.getOptionByName(name);
    }

    /**
     * 获取所有设置
     * @return
     */
    @Override
    @Cacheable(value="optionsCache",key="'optionsByname'")
    public List<OptionsDomain> getAllOptions() {
        return optionDao.getAllOptions();
    }
}
